package com.mhkjup.artcircle.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkjup.artcircle.db.YW_KptDB;
import com.mhkjup.artcircle.service.YW_KptService;
import com.mhkjup.artcircle.util.TimeUtil;

@Controller
@RequestMapping(value = "/YW_Kpt", method = RequestMethod.POST)
public class YW_KptConteoller extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "YW_KptServiceIml")
	private YW_KptService YW_KptService;

	@RequestMapping(value = "/addYW_KptDB", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addYW_KptDB(String KPNO, String TicketId,
			String TicketNm, String TicketUser, String TicketTime,
			String TicketAdrr, String ProductID, String ProductNM,
			String ProductEx, String ProductPrice, String ProUnit,
			String UnitNum, String ProCm, String ProYs, String ProBx,
			String ProKs, String ProPp, String Gys, String GysTel,
			String SaleNum, String KcNum, String Dpje, String SaleFx,
			String SaleType, String SaleJe, String FkType, String FkJe,
			String SzJe, String JzSta, String Jzdt, String FkDemo,
			String LastFkID, String Khnm, String KhTel, String KhAdrr,
			String FhType, String Fhwlgs, String Yfje, String SaleDemo,
			String UpYunDB, String Kpsta, HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		YW_KptDB ywdb = new YW_KptDB();
		Map<String, Object> map = new HashMap<String, Object>();
		ywdb.setOpIP(ip);

		ywdb.setKPNO(KPNO);
		// ywdb.setKPMXNO(KPMXNO);
		ywdb.setTicketId(TicketId);
		ywdb.setTicketNm(TicketNm);
		ywdb.setTicketUser(TicketUser);
		ywdb.setTicketTime(TicketTime);
		ywdb.setTicketAdrr(TicketAdrr);
		ywdb.setProductID(ProductID);
		ywdb.setProductNM(ProductNM);
		ywdb.setProductEx(ProductEx);
		ywdb.setProductPrice(ProductPrice);
		ywdb.setProUnit(ProUnit);
		ywdb.setUnitNum(UnitNum);
		ywdb.setProCm(ProCm);
		ywdb.setProYs(ProYs);
		ywdb.setProBx(ProBx);
		ywdb.setProKs(ProKs);
		ywdb.setProPp(ProPp);
		ywdb.setGys(Gys);
		ywdb.setGysTel(GysTel);
		ywdb.setmOption(TicketUser);
		ywdb.setSaleNum(SaleNum);
		ywdb.setKcNum(KcNum);
		ywdb.setDpje(Dpje);
		ywdb.setSaleFx(SaleFx);
		ywdb.setSaleType(SaleType);
		ywdb.setSaleJe(SaleJe);
		ywdb.setFkType(FkType);
		ywdb.setFkJe(FkJe);
		ywdb.setSzJe(SzJe);
		ywdb.setJzSta(JzSta);
		ywdb.setJzdt(Jzdt);
		ywdb.setFkDemo(FkDemo);
		ywdb.setLastFkID(LastFkID);
		ywdb.setKhnm(Khnm);
		ywdb.setKhTel(KhTel);
		ywdb.setKhAdrr(KhAdrr);
		ywdb.setFhType(FhType);
		ywdb.setFhwlgs(Fhwlgs);
		ywdb.setYfje(Yfje);
		ywdb.setSaleDemo(SaleDemo);
		ywdb.setUpYunDB(UpYunDB);
		ywdb.setDeleteFlg("0");
		ywdb.setMakeDt(TimeUtil.getCurrentTimeSceond());
		return YW_KptService.addYW_KptDB(ywdb);
	}

	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(String khnm, String khtel,
			String fktype, String rows, String pageSize) {
		return YW_KptService.getListYW_KptDB(khnm, khtel, fktype, rows,
				pageSize);
	}

	@RequestMapping(value = "/getYW_KptByYWNo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getYW_KptByYWNo(String KPNO, String kpxNo) {
		return YW_KptService.getListYW_KptDBBykpNo(KPNO, kpxNo);
	}

	@RequestMapping(value = "/delYW_KptByYWNo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delYW_KptByYWNo(String kpno) {
		return YW_KptService.delYW_Kpt(kpno);
	}

	@RequestMapping(value = "/updateYW_Kept", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateYW_Kept(String KPMXNO, String KPNO,
			String TicketId, String TicketNm, String TicketUser,
			String TicketTime, String TicketAdrr, String ProductID,
			String ProductNM, String ProductEx, String ProductPrice,
			String ProUnit, String UnitNum, String ProCm, String ProYs,
			String ProBx, String ProKs, String ProPp, String Gys,
			String GysTel, String SaleNum, String KcNum, String Dpje,
			String SaleFx, String SaleType, String SaleJe, String FkType,
			String FkJe, String SzJe, String JzSta, String Jzdt, String FkDemo,
			String LastFkID, String Khnm, String KhTel, String KhAdrr,
			String FhType, String Fhwlgs, String Yfje, String SaleDemo,
			String UpYunDB, String Kpsta, HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		YW_KptDB ywdb = new YW_KptDB();
		ywdb.setOpIP(ip);
		ywdb.setKPNO(KPNO);
		ywdb.setKPMXNO(KPMXNO);
		ywdb.setTicketId(TicketId);
		ywdb.setTicketNm(TicketNm);
		ywdb.setTicketUser(TicketUser);
		ywdb.setTicketTime(TicketTime);
		ywdb.setTicketAdrr(TicketAdrr);
		ywdb.setProductID(ProductID);
		ywdb.setProductNM(ProductNM);
		ywdb.setProductEx(ProductEx);
		ywdb.setProductPrice(ProductPrice);
		ywdb.setProUnit(ProUnit);
		ywdb.setUnitNum(UnitNum);
		ywdb.setProCm(ProCm);
		ywdb.setProYs(ProYs);
		ywdb.setProBx(ProBx);
		ywdb.setProKs(ProKs);
		ywdb.setProPp(ProPp);
		ywdb.setGys(Gys);
		ywdb.setGysTel(GysTel);
		ywdb.setmOption(TicketUser);
		ywdb.setSaleNum(SaleNum);
		ywdb.setKcNum(KcNum);
		ywdb.setDpje(Dpje);
		ywdb.setSaleFx(SaleFx);
		ywdb.setSaleType(SaleType);
		ywdb.setSaleJe(SaleJe);
		ywdb.setFkType(FkType);
		ywdb.setFkJe(FkJe);
		ywdb.setSzJe(SzJe);
		ywdb.setJzSta(JzSta);
		ywdb.setJzdt(Jzdt);
		ywdb.setFkDemo(FkDemo);
		ywdb.setLastFkID(LastFkID);
		ywdb.setKhnm(Khnm);
		ywdb.setKhTel(KhTel);
		ywdb.setKhAdrr(KhAdrr);
		ywdb.setFhType(FhType);
		ywdb.setFhwlgs(Fhwlgs);
		ywdb.setYfje(Yfje);
		ywdb.setSaleDemo(SaleDemo);
		ywdb.setUpYunDB(UpYunDB);
		ywdb.setMakeDt(TimeUtil.getCurrentTimeSceond());
		return YW_KptService.updateYW_KptDB(ywdb);
	}

	public static int checkCode() {

		// 生成随机6位码
		String s = "";
		while (s.length() < 6)
			s += (int) (Math.random() * 10);
		return Integer.valueOf(s);
	}
}
